package finalVersion;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;

import javax.swing.AbstractButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Component;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.Socket;

public class ConnectingGUI extends JFrame implements ActionListener {
	private JTextField IP1;
	private JTextField Port;
	private JTextField UserID;
	private JLabel Error;
	private String onMClick="Not connected";
	private JButton send;
	private JLabel lblListenport;
	private JTextField listenPort;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConnectingGUI frame = new ConnectingGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public ConnectingGUI() {
		initGUI();
		
	}
	
	/**
	 * Create the frame.
	 */
	
	private void initGUI(){
		setTitle("Connect To Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 366, 236);
		getContentPane().setLayout(null);
		
		JLabel lblServerip = new JLabel("SerVerIP");
		lblServerip.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblServerip.setBounds(10, 27, 60, 17);
		getContentPane().add(lblServerip);
		
		IP1 = new JTextField();
		IP1.setBounds(74, 24, 122, 20);
		getContentPane().add(IP1);
		IP1.setColumns(10);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPort.setBounds(206, 27, 46, 14);
		getContentPane().add(lblPort);
		
		Port = new JTextField();
		Port.setBounds(242, 26, 106, 20);
		getContentPane().add(Port);
		Port.setColumns(10);
		
		send = new JButton("Connect");
	
		send.setFont(new Font("Tahoma", Font.PLAIN, 13));
		send.setBounds(126, 117, 116, 32);
		getContentPane().add(send);
		send.addActionListener(this);
		
		
		JLabel lblUserid = new JLabel("UserID");
		lblUserid.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblUserid.setBounds(10, 86, 60, 17);
		getContentPane().add(lblUserid);
		
		UserID = new JTextField();
		UserID.setColumns(10);
		UserID.setBounds(74, 86, 274, 20);
		getContentPane().add(UserID);
		
		Error = new JLabel("");
		Error.setFont(new Font("Tahoma", Font.PLAIN, 13));
		Error.setBounds(10, 160, 266, 14);
		getContentPane().add(Error);
		
		lblListenport = new JLabel("ListenPort");
		lblListenport.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblListenport.setBounds(10, 58, 60, 17);
		getContentPane().add(lblListenport);
		
		listenPort = new JTextField();
		listenPort.setColumns(10);
		listenPort.setBounds(74, 55, 274, 20);
		getContentPane().add(listenPort);
		
		
	}		

	
	
	public void setError() {
		Error.setText("Status: Error connecting to Server");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Socket clientSocket;
		//get Use input
		String host = IP1.getText();
		String user = UserID.getText();				
		String port = Port.getText();
		String clientPort = listenPort.getText();
		
		try{
			//Create a new client GUI to connect to server
			clientSocket = new Socket(host, Integer.parseInt(port));
			ClientGUI clientGUI = new ClientGUI();
			clientGUI.setSocket(clientSocket);
			clientGUI.setUserID(user);
			clientGUI.setPort(clientPort);
			clientGUI.doconnect();
			clientGUI.setVisible(true);
			this.dispose();
		}
		catch (Exception ex){
			System.out.println(ex);
			Error.setText("Status : Error connecting to server");
		}	
	}
}
